package com.aiju.cash.ui.account.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.account.manager.AccountType;
import com.aiju.common.PixValue;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-30 11:52
 * @version: V1.0
 * @Description: 类目
 */
public class AccountTypeAdapter extends BaseAdapter {

    private Context context;
    private List<AccountType> items;
    private boolean drag;

    public void setDrag(boolean drag) {
        this.drag = drag;
        notifyDataSetChanged();
    }

    public AccountTypeAdapter(Context context, List<AccountType> datas) {
        this.context = context;
        this.items = datas;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final AccountType item = (AccountType) getItem(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_account_type, null);
            viewHolder.name = (TextView) convertView.findViewById(R.id.tv_account_type_name);
            viewHolder.pic = (ImageView) convertView.findViewById(R.id.iv_account_type_pic);
            viewHolder.select = (ImageView) convertView.findViewById(R.id.iv_account_type_select);
            viewHolder.drag = (ImageView) convertView.findViewById(R.id.drag_handle);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.name.setText(item.getName());
        viewHolder.pic.setImageResource(item.getPic());
        viewHolder.select.setSelected(item.isSelect());
        viewHolder.select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean select = !v.isSelected();
                v.setSelected(select);
                item.setSelect(select);
            }
        });
        if (drag) {
            viewHolder.drag.setVisibility(View.VISIBLE);
            convertView.setPadding(PixValue.dp.valueOf(15), 0, 0, 0);
        } else {
            convertView.setPadding(PixValue.dp.valueOf(15), 0, PixValue.dp.valueOf(15), 0);
            viewHolder.drag.setVisibility(View.GONE);
        }
        return convertView;
    }

    //删除指定位置的item
    public void remove(int arg0) {
        items.remove(arg0);
        this.notifyDataSetChanged();//不要忘记更改适配器对象的数据源
    }

    //在指定位置插入item
    public void insert(AccountType item, int arg0) {
        item.setPosition(arg0);
        items.add(arg0, item);
        this.notifyDataSetChanged();
    }


    class ViewHolder {
        ImageView pic;
        TextView name;
        ImageView select;
        ImageView drag;
    }
}
